# app/__init__.py
from flask import Flask
import pymysql
from flask_jwt_extended import create_access_token
from flask_jwt_extended import get_jwt_identity
from flask_jwt_extended import jwt_required
from flask_jwt_extended import JWTManager
from flask_sqlalchemy import SQLAlchemy
from flask_redis import FlaskRedis
db = SQLAlchemy()
jwt = JWTManager()
redis_store = FlaskRedis()  # 创建 Redis 实例
from app.routes.user import user
from flask_mqtt import Mqtt
from app.routes.mqtt import mqtt_blueprint, mqtt
# pymysql.install_as_MySQLdb()




def create_app():
    app = Flask(__name__)
    # # 配置 Flask-MQTT
    app.config['REDIS_URL'] = "redis://localhost:6379/0"
    app.config['MQTT_BROKER_URL'] = '120.78.165.0'
    app.config['MQTT_BROKER_PORT'] = 3050
    app.config['MQTT_USERNAME'] = 'admin'  # 如果有的话
    app.config['MQTT_PASSWORD'] = 'test'  # 如果有的话
    app.config['MQTT_REFRESH_TIME'] = 1.0  # 刷新时间（单位：秒）
    app.config["JWT_SECRET_KEY"] = "%&*huang!!!plxiuling"  # 设置 jwt 的秘钥
    # 根据你的 MySQL 配置修改以下信息
    app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:201658@localhost:3306/mysql'
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False  # 可选，建议关闭跟踪修改
    redis_store.init_app(app)
    mqtt.init_app(app)
    jwt.init_app(app)
    # 注册蓝图
    app.register_blueprint(user)
    app.register_blueprint(mqtt_blueprint)
    db.init_app(app)
    return app
